//
//  MHRedPacketRecordViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/11/3.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHRedPacketRecordViewModel.h"
#import "MHSingleRecordlist.h"
#import "MHList.h"
@implementation MHRedPacketRecordViewModel



-(void)initialize
{
    [super initialize];
    self.title = @"红包记录";
    self.shouldPullDownToRefresh = NO;
    self.shouldPullUpToLoadMore = NO;
    self.prefersNavigationBarBottomLineHidden = YES;
    self.translucentNavigationBar = YES;
    self.changeNavTitle = YES;
    @weakify(self);
    RAC(self, datas) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
    /// 数据源
    RAC(self,dataSource) = [RACObserve(self, datas) map:^(NSArray * blacks) {
        @strongify(self)
        MHCommonGroupViewModel *group1 = [MHCommonGroupViewModel groupViewModel];
        group1.itemViewModels = [self dataSourceWithcircleMembers:blacks];
        return @[group1];
    }];
    
    
}
- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        self.redPacketId  = [params[MHViewModelIDKey] intValue];
    }
    return self;
}


- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page{
    
    NSMutableArray * (^mapcircleMembers)(MHSingleRecordlist *) = ^(MHSingleRecordlist *list) {
        self.list = list;
        return [list.recordList mutableCopy];
    };
    return [[[self.services.client openRedPacketWithRedId:self.redPacketId] mh_parsedResults] map:mapcircleMembers];
}

#pragma mark - 辅助方法
- (NSArray *)dataSourceWithcircleMembers:(NSArray *)datas {
    if (MHObjectIsNil(datas) || datas.count == 0) return nil;
    NSMutableArray *viewModels = [[datas.rac_sequence map:^(MHList *list) {
        MHCommonItemViewModel *viewModel  = [MHCommonItemViewModel itemViewModelWithTitle:list.userName  icon:list.fullUserFace];
        viewModel.subtitle = [NSString stringWithFormat:@"%.2f元",list.amount];
        viewModel.leftDetialTile = list.created;
        viewModel.model = list;
        return viewModel;
    }].array mutableCopy];
    return viewModels ?: @[] ;
}

@end
